package com.stlm2.controller;

import com.stlm2.core.common.page.PageView;
import com.stlm2.dems.entity.Entrance;
import com.stlm2.dems.entity.User;
import com.stlm2.dems.service.EntranceService;
import com.stlm2.dems.service.UserService;
import com.stlm2.util.StringUtils;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Author：fuchunyu
 * Date：2017/10/20
 * Description：
 */
@Controller
@RequestMapping("entranceC")
public class EntranceController {
    @Resource
    private UserService userService;
    @Resource
    private EntranceService entranceService;

    //查询
    @ResponseBody
    @RequestMapping(value = "/searchEntrance.json",produces = "application/json;charset=UTF-8")
    public String searchEntrance(String token,ModelMap model, String page,String typeValue){
        String data=null;
        try {
            data=entranceService.searchEntrance(token,model,page,typeValue);
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"查询失败",data);
        }
        return  data;
    }

    @ResponseBody
    @RequestMapping(value="/getDetails.json",produces="application/json;charset=UTF-8")
    public  String  getDetails(ModelMap model,String token,String id) {
        String data=null;
        try {
            data=entranceService.getDetails( model,token,id);
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"查询失败",data);
        }
        return  data;
    }

    /**
     * Author：fuchunyu
     * Date：2017/10/18
     * Description：添加
     */
    @ResponseBody
    @RequestMapping(value = "/addEntrance.json",produces = "application/json;charset=utf-8")
    public String addEntrance(String token,String name,String shortName,String appUrl,String img,String url){
        User user=userService.getByToken(token);
        if (user==null)
            return CodeContent.returnLogin();
        if(StringUtils.isNullOrTrimEmpty(name))
            return CodeContent.returnFailure("名称不能为空！");
        if(StringUtils.isNullOrTrimEmpty(shortName))
            return CodeContent.returnFailure("简介不能为空！");
        if(StringUtils.isNullOrTrimEmpty(img))
            return CodeContent.returnFailure("图片不能为空！");
        if(StringUtils.isNullOrTrimEmpty(url))
            return CodeContent.returnFailure("链接不能为空！");
        Entrance entrance=new Entrance();
        entrance.setName(name);
        entrance.setShortName(shortName);
        if(StringUtils.isNotNullAndEmpty(appUrl))
             entrance.setAppUrl(appUrl);
        entrance.setUrl(url);
        entrance.setImg(img);
        entrance.setState(1);
        entrance.setCreateUserId(user.getId());
        entrance.setCreateTime(new Date());

        entranceService.addEntity(entrance);
        return CodeContent.returnSuccess(null);
    }

    /**
     * Author：fuchunyu
     * Date：2017/10/18
     * Description：更新
     */
    @ResponseBody
    @RequestMapping(value = "/updateEntrance.json",produces = "application/json;charset=utf-8")
    public String updateEntrance(ModelMap model,String token,String id,String name,String shortName,String appUrl,String img,String url,boolean state) {
        String data=null;
        try {
            data=entranceService.updateEntrance(model,token,id,name,shortName,appUrl,img,url,state);
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"修改失败",data);
        }
        return  data;
    }
}
